package Model;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import Entity.*;
import S2.server.ConnectionManage;


public class ProductModel {

	private static Connection conn = ConnectionManage.getInstance()
			.getConnection();


	public static boolean addProduct(String Id,String name, String desc, String price, String picture) throws SQLException, IOException {
		
		String sql = "insert into MyPictures(id, name, desc, price, picture) values (?, ?, ?,?,?)";

	    FileInputStream fis = null;
	    PreparedStatement ps = null;
	    try {
	      conn.setAutoCommit(false);
	      File file = new File(picture);
	      fis = new FileInputStream(file);
	      ps = conn.prepareStatement(sql);
	      ps.setString(1, Id);
	      ps.setString(2, name);
			ps.setString(3, desc);
			ps.setString(4, price);
	      ps.setBinaryStream(5, fis, (int) file.length());
	      
	      
	      boolean res = ps.execute();
	     // conn.commit();
	      return !res;
	      
	     
	    } catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} finally {
	      ps.close();
	      fis.close();
	    }
	    
	   // return true;
		/*
		String sql = "INSERT INTO products (id,name,description,price,picture)"+"VALUES(?,?,?,?,?)";
		ResultSet rs = null;
		Product bean =  new Product();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setString(3, desc);
			stmt.setFloat(4, price);
			stmt.setString(5, picture);
			
			boolean res = stmt.execute();
			return !res;

		} catch (SQLException e) {
			System.err.println(e);
			return false;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}*/

	}
	
	public static Product UpdateProduct(String Id,String name, String desc, String price, String picture) throws SQLException {
		String sql = "Update INTO product(id,name,description,price,picture)"+"VALUES(?,?,?,?,?)";
		ResultSet rs = null;
		Product bean =  new Product();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setString(3, desc);
			stmt.setString(4, price);
			stmt.setString(5, picture);
			//--------------------------
			
			
			
			//-----------------------------
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				bean.setId(Id);
				bean.setName(name);
				bean.setDescription(desc);
				bean.setPrice(price);
				bean.setPicture(picture);
				return bean;
						}
				else return null;
	}
	 catch (SQLException e) {
		System.err.println(e);
		return null;
	} finally {
		if (rs != null) {
			rs.close();
		}
	}
	
	
}
	public static boolean DeleteProduct(String name,String Id) throws SQLException {
		String sql = "DELETE FROM Product " +  "WHERE name = ? AND id=?";
		ResultSet rs = null;
		
		try (PreparedStatement stmt = conn.prepareStatement(sql);)
		{		
			stmt.setString(1, name);
			stmt.setString(2, Id);
			boolean res = stmt.execute();
			return !res;
		}
		 catch (SQLException e) {
				System.err.println(e);
				
			} finally {
				if (rs != null) {
					rs.close();
				}
			
		
			}
		return true;
}
	public static Product getProduct(String name, String Id) throws SQLException {
		String sql = "SELECT * FROM" + " Product WHERE" + " name=? AND id=?";
		ResultSet rs = null;
		Product bean =  new Product();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, name);
			stmt.setString(2, Id);
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				bean.setId(Id);
				bean.setName(name);
				bean.setDescription(rs.getString("description"));
				bean.setPrice(rs.getString("price"));
				bean.setPicture(rs.getString("pricture"));
				return bean;
						}
				else return null;

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
}
}